package net.hn.hnms.biz.plan.company.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.hn.hnms.biz.plan.company.service.CompanyStatisticService;
import net.hn.hnms.biz.plan.domain.CoalWeekCount;
import net.hn.hnms.biz.plan.request.BaseReq;
import net.hn.hnms.biz.plan.service.CoalWeekCountService;
import net.hn.hnms.biz.plan.vo.CoalWeekCountVo;
import net.hn.hnms.sys.common.core.domain.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.ArrayList;
import java.util.List;

/**
 * 煤炭产量周统计
 */
@RestController
@Api(value = "生产决策", tags = {"获取周产量列表-获取周进尺列表"})
@RequestMapping("/company/coalweekcount")
public class CoalWeekCountController {

    @Resource
    private CoalWeekCountService coalWeekCountService;
    @Autowired
    private CompanyStatisticService companyStatisticService;

    @ApiOperation("获取周产量列表")
//    @SaCheckPermission("plan:output:list")
    @GetMapping("/list")
    public R<List<CoalWeekCountVo>> list(BaseReq baseReq) {
        if (baseReq.getDateTimeRange() == null){
            LocalDate now = LocalDate.now();
            WeekFields weekFields = WeekFields.ISO;
            LocalDate mondayDate = now.with(weekFields.dayOfWeek(), 1L);
            LocalDate sundayDate = now.with(weekFields.dayOfWeek(), 7L);
            ArrayList<String> strings = new ArrayList<>();
            strings.add(mondayDate.toString());
            strings.add(sundayDate.toString());
            baseReq.setDateTimeRange(strings);
        }
        List<CoalWeekCount> list = coalWeekCountService.selectList(baseReq);

        ArrayList<CoalWeekCountVo> coalWeekCountVos = new ArrayList<>();
        CoalWeekCountVo coalWeekCountVo = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (CoalWeekCount coalWeekCount : list) {
            coalWeekCountVo = new CoalWeekCountVo();
            coalWeekCountVo.setWeekDate(format.format(coalWeekCount.getMonDate())+"~"+format.format(coalWeekCount.getSunDate()));
            BeanUtils.copyProperties(coalWeekCount,coalWeekCountVo);
            coalWeekCountVo.setName(coalWeekCount.getMineCode());
            coalWeekCountVos.add(coalWeekCountVo);
        }
        return R.ok(coalWeekCountVos);
    }
    @ApiOperation("获取周进尺列表")
//    @SaCheckPermission("plan:output:list")
    @GetMapping("/listfootage")
    public R<List<CoalWeekCountVo>> listfootage(BaseReq baseReq) {
        if (baseReq.getDateTimeRange() == null){
            LocalDate now = LocalDate.now();
            WeekFields weekFields = WeekFields.ISO;
            LocalDate mondayDate = now.with(weekFields.dayOfWeek(), 1L);
            LocalDate sundayDate = now.with(weekFields.dayOfWeek(), 7L);
            ArrayList<String> strings = new ArrayList<>();
            strings.add(mondayDate.toString());
            strings.add(sundayDate.toString());
            baseReq.setDateTimeRange(strings);
        }
        List<CoalWeekCount> list = coalWeekCountService.selectListfootage(baseReq);

        ArrayList<CoalWeekCountVo> coalWeekCountVos = new ArrayList<>();
        CoalWeekCountVo coalWeekCountVo = null;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        for (CoalWeekCount coalWeekCount : list) {
            coalWeekCountVo = new CoalWeekCountVo();
            coalWeekCountVo.setWeekDate(format.format(coalWeekCount.getMonDate())+"~"+format.format(coalWeekCount.getSunDate()));
            BeanUtils.copyProperties(coalWeekCount,coalWeekCountVo);
            coalWeekCountVo.setName(coalWeekCount.getMineCode());
            coalWeekCountVos.add(coalWeekCountVo);
        }
        return R.ok(coalWeekCountVos);
    }

    @ApiOperation("定时计算周产量进尺据并插入，每天凌晨3点运行")
    @Scheduled(cron = "30 58 23 * * ? ")
    @GetMapping("/calculatedWeeklyOutput")
    public R<Void> add() {
        System.out.println("-----------------------------------执行定时--------------------------");
        List<String> mineCodes = companyStatisticService.selectMineCode();
        List<String> companyCodes = companyStatisticService.selectcompanyCode();
        if (mineCodes!=null){
            for (String mineCode : mineCodes) {
                System.out.println(mineCode+"開始執行");
                coalWeekCountService.calculatedWeeklyOutput(mineCode,null);
                coalWeekCountService.calculatedWeeklyFootage(mineCode,null);
            }
        }
        for (String companyCode : companyCodes) {
            coalWeekCountService.calculatedWeeklyOutput(null,companyCode);
            coalWeekCountService.calculatedWeeklyFootage(null,companyCode);
        }

        System.out.println("-----------------------------------执行完成--------------------------");
        return R.ok();
    }
}
